package ee.domain;

import com.opensymphony.xwork2.ActionSupport;
import ee.db.ProjectService;
import ee.db.UserService;
import ee.model.Assignment;
import ee.model.Project;
import org.apache.struts2.convention.annotation.Action;

import javax.annotation.Resource;
import java.util.Collection;

public class Projects extends ActionSupport{
  @Resource
  private ProjectService projectService;
  @Resource
  private UserService userService;
  private Object[] projectNames;
  public String projectName, username;
  private Project currentProject;
  public String id;
  public String topic, deadLine, priority, comments, tags, state;
  private boolean saved;

  @Action(value = "getProject", results = {
  })
  public String getProject() {
    this.currentProject = projectService.getProject(projectName, username);
    return SUCCESS;
  }

  @Action(value = "newTodo", results = {
  })
  public String newTodo() {
    this.id = projectService.getNewAssignmentId(projectName, username);
    return "id";
  }

  @Action(value = "saveTodo")
  public String saveTodo() {
    Assignment assignment = new Assignment();
    assignment.setId(id);
    assignment.setTopic(topic);
    assignment.setDeadline(deadLine);
    assignment.setComments(comments);
    assignment.setPriority(priority);
    assignment.setStatus(state);
    assignment.setTags(tags);
    projectService.saveAssignment(assignment);
    saved = true;
    return "saved";
  }

  @Action(value = "deleteTodo")
  public String deleteTodo() {
    projectService.deleteAssignment(id);
    saved = true;
    return "saved";
  }

  public String changeState() {
    projectService.changeState(id, state);
    saved = true;
    return "saved";
  }

  public String createProject() {
    userService.createProject(username, projectName);
    saved = true;
    return "saved";
  }

  public String getProjects() {
    projectNames = projectService.getProjectNames(username);
    return "projects";
  }


  public Collection<Assignment> getCurrentProject() {
    return this.currentProject.getAssignments();
  }

  public String getId() {
    return id;
  }

  public boolean getSaved() {
    return saved;
  }

  public Object[] getProjectNames() {
    return projectNames;
  }
}
